package academic.ui.client.widgets.forms;

import java.util.Date;

import ui.client.GenericContainerTag;
import ui.client.widgets.InputField;
import ui.client.widgets.TextAreaInput;
import academic.ui.client.jquery.JQuery;
import academic.ui.client.rules.Rules;
import academic.ui.client.widgets.DateTimePicker;

import com.google.gwt.event.dom.client.HasKeyDownHandlers;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.HasText;

import ecommerce.soco.shared.GWT_Event;
import ecommerce.soco.shared.GWT_Product;

public class CreateEventForm extends FormWidget {

	/*
	 * params['name'] = name;
		params['description'] = description;
		params['start_time'] = start_time;//A JSON string containing an ISO-8601 formatted date/time
		params['end_time'] = end_time;//A JSON string containing an ISO-8601 formatted date/time
		params['location'] = location;
		params['venue'] = venue; //A JSON object containing street, city, state, zip, country, latitude, and longitude fields
		
	 */
	private InputField eventName = new InputField("name");
	private TextAreaInput description = new TextAreaInput("description");
	/*
	 * <p>
    <strong>From:</strong>
    <input id="dt_date_start" type="text" />
    <input id="dt_time_start" type="text" />
	</p>
	<p>
	    <strong>To:</strong>
	    <input id="dt_date_end"   type="text" />
	    <input id="dt_time_end"   type="text" />
	</p>
	 */
	//private InputField starttime = new InputField("start time","starttime","right now");
	//private InputField endtime = new InputField("end time","endtime","");
	private DateTimePicker dateTimePicker = new DateTimePicker();
	private InputField location = new InputField("location");
	private InputField venue = new InputField("venue");
	private KeyDownHandler removeErrors = new KeyDownHandler() {

		@Override
		public void onKeyDown(KeyDownEvent event) {
			JQuery.fadeOutErrors();
		}
		
	};
	
	public CreateEventForm() {
		super();
		div.setStyleName("createEvent");
		div.add(eventName);
		div.add(dateTimePicker);
		div.add(description);
		
		div.add(venue);
		div.add(location);
		eventName.getField().setStyleName("longInput");
		description.setStyleName("longInput");		
		div.add(errorMsgs);
		eventName.getField().addKeyDownHandler(removeErrors);
		description.getField().addKeyDownHandler(removeErrors);
		location.getField().addKeyDownHandler(removeErrors);
		venue.getField().addKeyDownHandler(removeErrors);
	}
	
	@Override
	public Boolean checkRules() {
		errorMsgs.clear();
		if("".equals(eventName.getText()))
		{
			eventName.addStyleName("missingValue");
			errorMsgs.add(new GenericContainerTag("div","Email is required."));
		}
		return errorMsgs.getWidgetCount()<1; 
	}

	@Override
	public void removeErrors() {
		errorMsgs.clear();
		eventName.removeStyleName("missingValue");
		description.removeStyleName("missingValue");
		eventName.removeStyleName("invalid");
	}

	public HasText getMessage() {
		return description;
	}

	public HasKeyDownHandlers getEmailKeyDown() {
		return eventName.getField();
	}

	public void reset() {
		removeErrors();
		eventName.setText("");
		eventName.getField().setEnabled(true);
		description.setText("");
		description.getField().setEnabled(true);
	}

	public HasKeyDownHandlers getMessageKeyDown() {
		return description.getField();
	}

	public void initForm(GWT_Product product) {
		eventName.setText(product.getName()+" what a great deal!");
		location.setText(product.getMerchant().getAddress());
		venue.setText(product.getMerchant().getName());
	}
	
	public String getEventName() {
		return eventName.getText();
	};

	public String getDescription() {
		return description.getText();
	};
	
	public DateTimePicker getDatePicker() {
		return dateTimePicker;
	}

	public GWT_Event getEvent() {
		GWT_Event event = new GWT_Event();
		event.setName(eventName.getText());
		event.setDescription(description.getText());
		event.setStartdate(dateTimePicker.getStartDateTime());
		event.setEnddate(dateTimePicker.getEndDateTime());
		event.setLocation(location.getText());
		event.setVenue(venue.getText());
		return event;
	}
	
}
